package examples.jsf.lifecycle.impl;

import examples.jsf.FacesException;
import examples.jsf.component.UIViewRoot;
import examples.jsf.context.FacesContext;
import examples.jsf.event.PhaseId;
import examples.jsf.event.PhaseListener;
import examples.jsf.event.PostRestoreStateEvent;
import examples.jsf.lifecycle.Lifecycle;

import java.util.ListIterator;

/**
 * @author Bill
 * @version 2013-05-29
 */
public class RestoreViewPhase extends Phase {

    @Override
    public void doPhase(FacesContext context, Lifecycle lifecycle, ListIterator<PhaseListener> phaseListeners) {
        super.doPhase(context, lifecycle, phaseListeners);    //To change body of overridden methods use File | Settings | File Templates.

        // 处理 ViewRoot的 PhaseListener 监听（如果设置了的话）
    }

    @Override
    public void execute(FacesContext context) throws FacesException {

        // 各种常规判空检查

        UIViewRoot viewRoot = context.getViewRoot();
        if (null != viewRoot) {

            // 1. 设置view locale

            // 2. 广播state restore event
            PostRestoreStateEvent postRestoreStateEvent = new PostRestoreStateEvent(viewRoot);

            // 如果没有输出内容， 就直接走输出了
//            if (!facesContext.isPostback()) {
//                facesContext.renderResponse();
//            }

        }

        // 对ViewRoot进行一系列处理
    }

    @Override
    public PhaseId getId() {
        return PhaseId.RESTORE_VIEW;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
